from selenium import webdriver
from selenium.webdriver.chrome.options import Options # => 引入Chrome的配置
from selenium.webdriver.chrome.service import Service
import time
import json

# 配置
ch_options = Options()
ch_options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
ch_options.add_argument('--ignore-certificate-errors')
ch_options.add_experimental_option('useAutomationExtension', False)
# 去掉黄条
ch_options.add_experimental_option('excludeSwitches', ['enable-automation'])
ch_options.add_argument('--ignore-certificate-errors-spki-list')

ch_options.add_argument('--no-sandbox')

ch_options.add_experimental_option('perfLoggingPrefs', {'enableNetwork': True})

#ch_options.add_argument("--headless")  # => 为Chrome配置无头模式

# chromedriver 路径配置
driverP = Service('chromedriver.exe')
# 在启动浏览器时加入配置
driver = webdriver.Chrome(service= driverP, options=ch_options) # => 注意这里的参数 

driver.get('http://baidu.com')

logs = driver.get_log("performance")

for log in logs:
    print(log["message"])
    logjson = json.loads(log["message"])["message"]
    if logjson['method'] == 'Network.responseReceived':
        continue
        params = logjson['params']
        try:
            requestUrl = params['response']['url']

            if "/bdsp/album/pay" in requestUrl:

                requestId = params['requestId']
                response_body = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': requestId})
                _content = json.loads(response_body["body"])
            else:
                continue
        except:
            requestUrl = "None"
            logger.info("没找到requestUrl：{}".format(requestUrl))
            continue
    else:
        continue
#driver.find_element(value = 'kw').send_keys('测试')
#driver.find_element(value = 'su').click()

time.sleep(2)


# 只有截图才能看到效果咯
driver.save_screenshot('./ch.png')

driver.quit()